package fstvis;

public class Field1D {
	double[] fld;
	int length;

	public int getLength() {
		return length;
	}

	public Field1D(Field fld2d, int ni, int nj) {
		double[][] f = fld2d.getField();
		int x = 0;

		length = ni*nj;
		fld = new double[length];
		
		for (int i=0; i<ni; i++) {
			for (int j=0; j<nj; j++) {
				x = Field1D.convert2DIndexTo1D(ni, nj, i, j);
				fld[x] = f[i][j];
			}
		}
	}

	public double value(int x) throws IndexOutOfBoundsException {
		if (x >= 0 && x < length) {
			return this.fld[x];
		} else {
			throw new IndexOutOfBoundsException(x + " is out of bounds [0, " + length + "[");
		}
	}
	
	public double[] getField() {
		return this.fld;
	}

	public static int convert2DIndexTo1D(int ni, int nj, int i, int j) {
		return nj*i + j;
	}

	public static int[] convert1DIndexTo2D(int ni, int nj, int x) {
		double x_dbl = (double)x;
		double nj_dbl = (double)nj;
		int[] point = new int[2];

		point[0] = (int)Math.floor(x_dbl/nj_dbl);
		point[1] = (int)(x % nj);
		
		return point;
	}
}
